/**
 * @see 该页面是通用列表界面，此界面从不同的业务进入，需要带入参数进入。进入时根据业务类型来做不同的处理。
 * @see 接受参数主要有：
 * @param {*type 业务类型（委托打印、预约打印、...）} String
 * @example /pages/workService/components/sendEmail/index?type=电子证明
 */

const entrustPrintTitle = {
  printTypeName: '文件名称', // 打印文件
  createOrderTime: '委托时间', // 受委托人
  beEntrutsUserName: '委托人',
  orderStatus: '状态' // 状态
}

const printLogTitle = {
  fileName: '文件名称',
  printTime: '打印时间',
  printNumber: '份数',
  money: '金额',
  fileType: '文件类型'
}

let paging = {
  page: 1,
  size: 15,
  total: 0
}

Page({
  data: {
    title: {},
    list: [],
    type: '',
    appMsg: {
      type: '',
      show: false
    },
    oOrderDetails: {} // 申请记录内容
  },
  onLoad(option) {
    const { type } = option
    switch (type) {
      case '委托打印':
        this.setData({
          type,
          title: entrustPrintTitle
        })
        this.getEntrustPrintList()
        break
      case '打印日志':
        this.setData({
          type,
          title: printLogTitle
        })
        this.getPrintLog()
        break
      default:
        wx.showToast({
          title: '参数错误',
          icon: 'none',
          duration: 2000
        })
    }
  },
  onUnload() {
    paging = {
      page: 1,
      size: 15,
      total: 0
    }
  },
  /**
   * @see 获取打印日志
   * @see 徐州学院
   */
  getPrintLog() {
    const request = {
      page: paging.page,
      size: paging.size
    }
    API['workService/getPrintLog'](request).then((result) => {
      wx.stopPullDownRefresh()
      this.setData({
        appMsg: {
          type: 'loading',
          show: false
        }
      })
      const { data, totalPageNum } = result
      paging.total = totalPageNum
      if (data.length === 0) {
        this.setData({
          appMsg: {
            type: 'none',
            show: true
          }
        })
      } else {
        data.forEach((item) => {
          item.printTime = item.printTime.split(' ')[0]
        })
        this.setData({
          list: this.data.list.concat(data)
        })
      }
    }).catch((error) => {
      wx.stopPullDownRefresh()
      this.setData({
        appMsg: {
          type: 'err',
          show: true
        }
      })
    })
  },
  /**
   * @see 获取委托打印申请记录
   */
  getEntrustPrintList() {
    wx.$API['workService/entrustPrintList']({}).then((result) => {
      wx.stopPullDownRefresh()
      this.setData({
        appMsg: {
          type: 'loading',
          show: false
        }
      })
      const { data, total } = result
      paging.total = total
      if (data.length === 0) {
        this.setData({
          appMsg: {
            type: 'none',
            show: true
          }
        })
      } else {
        data.forEach((item) => {
          switch (item.orderStatus) {
            case 1:
              item.orderStatusText = '待付款'
              break
            case 2:
              item.orderStatusText = '已付款'
              break
            case 3:
              item.orderStatusText = '处理中'
              break
            case 4:
              item.orderStatusText = '交易失败'
              break
            case 5:
              item.orderStatusText = '已取消'
              break
            default:
              item.orderStatusText = '无'
          }
        })
        this.setData({
          list: this.data.list.concat(data)
        })
      }
    }).catch((error) => {
      wx.stopPullDownRefresh()
      this.setData({
        appMsg: {
          type: 'err',
          show: true
        }
      })
    })
  },
  /**
   * @see 监听页面滚动到底部加载更多
   */
  onReachBottom() {
    if (paging.total <= this.data.list.length) {
      return
    } else {
      paging.page++
      switch (this.data.type) {
        case '委托打印':
          // this.getEntrustPrintList()
          break
        case '打印日志':
          this.getPrintLog()
          break
      }
    }
  },
  /**
   * @see 重置数据
   */
  refreshData() {
    paging = {
      page: 1,
      size: 15,
      total: 0
    }
    this.setData({
      list: []
    })
    switch (this.data.type) {
      case '委托打印':
        this.getEntrustPrintList()
        break
      case '打印日志':
        this.getPrintLog()
        break
    }
  },
  clickMsgButton() {
    this.refreshData()
  },
  /**
   * @see 上拉刷新
   */
  onPullDownRefresh () {
    this.refreshData()
  }
})
